from datetime import time
from flask import Blueprint,jsonify
from util.logger_utils import setup_logger
from .handler.callbackHandler import json_callback
from ..command.menu_command import menu_command

logger = setup_logger('web回调日志')
callback = Blueprint('callback', __name__)

@callback.route('/callback/message',methods=['GET','POST'])
def callback_message():
    # 处理微信回调消息 json格式
    message = json_callback()
    msgType = message['MsgType']
    content = message['Content']
    from_user = message['FromUserName']
    to_user = message['ToUserName']
    if msgType == 'text':
        msg = menu_command(content)
        if msg:
            jsonify({
                'ToUserName':from_user,
                'FromUserName':to_user,
                'CreateTime':int(time.time()),
                'MsgType':'text',
                'Content':msg
            })
    return 'success',200    